#include <QtGui>
#include "renderarea.h"


//RenderArea::RenderArea(QWidget *parent): QWidget(parent)
//! RenderArea constructor.
RenderArea::RenderArea(Register* r)
{
    reg=r;
    antialiased = false;

    //setBackgroundRole(QPalette::Base);
    setBackgroundRole(QPalette::Window);
    setAutoFillBackground(true);
    setFixedSize(402,50);
}

//! When the mouse is pressed, switch to the field tab which is clicked
void RenderArea::mousePressEvent ( QMouseEvent * event ) {
    //should be ok. TODO: highlight the current field?
    QPoint pos(event->pos());

    int j= (pos.x()-9)/12;

    int k;

    if(reg->getNotation()==Qt::RightToLeft)
        k=31-j;
    else
        k=j;


    if(k>31 || pos.x()-9<0 || reg->taken[k]==-1)
        return;

    for(int i=0; i<reg->getGroups()->length();i++)
        if(reg->getGroups()->at(i)->getId()==reg->taken[k])
            this->reg->getTabs()->setCurrentWidget(reg->getGroups()->at(i));


}




QSize RenderArea::minimumSizeHint() const
{
    return QSize(100, 50);
}

QSize RenderArea::sizeHint() const
{
    return QSize(200, 80);
}



void RenderArea::setPen(const QPen &pen)
{
    this->pen = pen;
    update();
}


//! Set a brush
void RenderArea::setBrush(const QBrush &brush)
{
    this->brush = brush;
    update();
}

//!
void RenderArea::setAntialiased(bool antialiased)
{
    this->antialiased = antialiased;
    update();
}


//!
void RenderArea::setTransformed(bool transformed)
{
    this->transformed = transformed;
    update();
}


//! Called everytime a QPaintEvent occurs
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
    //x,y,width,height
    QRect rect(5, 5, 10, 20);

    QPainter painter(this);
    //painter.setPen(pen);
    //painter.setBrush(brush);
    //painter.setRenderHint(QPainter::Antialiasing, true);

    painter.setBrush(palette().dark().color());

    painter.setPen(palette().dark().color());

    QLinearGradient linearGradient(0, 0, 100, 100);
    linearGradient.setColorAt(0.0, Qt::white);
    linearGradient.setColorAt(0.2, Qt::darkYellow);
    linearGradient.setColorAt(1.0, Qt::green);
    painter.setBrush(linearGradient);


    painter.save();

    if(reg->getNotation()==Qt::RightToLeft){

        painter.translate(5, 0);
        for (int i = 0; i < 32; i++) {

            if(reg->taken[31-i]==-1)
                painter.setBrush(palette().shadow().color());
            else
            {
                if(reg->readOnly[31-i]==true)
                {   linearGradient.setColorAt(0.0, Qt::white);
                    linearGradient.setColorAt(0.2, Qt::darkYellow);
                }
                else{
                    linearGradient.setColorAt(0.0, Qt::white);
                    linearGradient.setColorAt(0.2, Qt::red);
                }
                painter.setBrush(linearGradient);}

            painter.drawRect(rect);

            painter.setPen(QPen(QPalette::Text));

            if(reg->taken[31-i]!=-1)
                painter.drawText(rect,Qt::AlignCenter, QString::number(reg->taken[31-i]));

            if(reg->taken[31-i]!=reg->taken[31-(i+1)] && i<31)
                painter.drawLine(16,6,16,24);
            if(i%4==3)
                painter.drawLine(16,25,16,28);

            painter.setPen(Qt::red);
            
            if(reg->getTabs()->currentIndex()!=-1 && reg->taken[31-i]==reg->getGroups()->at(reg->getTabs()->currentIndex())->getId())
            {painter.drawLine(4,3,15,3);}
            
            painter.setPen(palette().dark().color());
            painter.translate(12, 0);


        }

        //    paint LEGENDA
        //    painter.translate(10,6);
        //
        //    painter.setBrush(Qt::red);
        //    painter.drawRect(QRect(0, 0, 5, 5));
        //
        //    painter.translate(0,8);
        //
        //    painter.setBrush(Qt::darkYellow);
        //    painter.drawRect(QRect(0, 0, 5, 5));
        //
        //    painter.translate(0,8);
        //    painter.setBrush(palette().dark().color());
        //    painter.drawRect(QRect(0, 0, 5, 5));



        painter.restore();

        painter.save();
        painter.translate(5, 0);
        painter.setPen(QPen(QPalette::Text));

        painter.translate(6,26);
        for (int i = 1; i <= 32; i++) {
            if(i%4==0)
                painter.drawText(0,2,20,20, Qt::AlignCenter, QString::number(32-i,10));
            painter.translate(12, 0);
        }

    }else////////////////////////////////////////////////////// LEFT to RIGHT  //////////////
    {
        painter.translate(5, 0);
        painter.setPen(QPen(QPalette::Text));
        painter.drawLine(4,25,4,28);
        painter.setPen(palette().dark().color());
        for (int i = 0; i < 32; i++) {

            if(reg->taken[i]==-1)
                painter.setBrush(palette().shadow().color());
            else
            {
                if(reg->readOnly[i]==true)
                {   linearGradient.setColorAt(0.0, Qt::white);
                    linearGradient.setColorAt(0.2, Qt::darkYellow);
                }
                else{
                    linearGradient.setColorAt(0.0, Qt::white);
                    linearGradient.setColorAt(0.2, Qt::red);
                }
                painter.setBrush(linearGradient);}

            painter.drawRect(rect);

            painter.setPen(QPen(QPalette::Text));

            if(reg->taken[i]!=-1)
                painter.drawText(rect,Qt::AlignCenter, QString::number(reg->taken[i]));

            if(reg->taken[i]!=reg->taken[(i+1)] && i<31)
                painter.drawLine(16,6,16,24);
            if(i%4==3 && i<31)
                painter.drawLine(16,25,16,28);

            painter.setPen(Qt::red);

            if(reg->getTabs()->currentIndex()!=-1 && reg->taken[i]==reg->getGroups()->at(reg->getTabs()->currentIndex())->getId())
            {painter.drawLine(4,3,15,3);}


            painter.setPen(palette().dark().color());

            painter.translate(12, 0);


        }

        //    LEGENDA
        //    painter.translate(10,6);
        //
        //    painter.setBrush(Qt::red);
        //    painter.drawRect(QRect(0, 0, 5, 5));
        //
        //    painter.translate(0,8);
        //
        //    painter.setBrush(Qt::darkYellow);
        //    painter.drawRect(QRect(0, 0, 5, 5));
        //
        //    painter.translate(0,8);
        //    painter.setBrush(palette().dark().color());
        //    painter.drawRect(QRect(0, 0, 5, 5));



        painter.restore();

        painter.save();

        painter.setPen(QPen(QPalette::Text));

        painter.translate(0,26);
        for (int i = 0; i < 32; i++) {
            if(i%4==0)
                painter.drawText(0,2,20,20, Qt::AlignCenter, QString::number(i,10));
            painter.translate(12, 0);
        }




    }


    painter.restore();
    painter.setRenderHint(QPainter::Antialiasing, false);
    painter.setPen(palette().dark().color());
    painter.setBrush(Qt::NoBrush);
    painter.drawRect(QRect(0, 0, width() - 1, height() - 1));






}

